<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="字段类型" prop="type">
        <el-input v-model="dataForm.fieldType" placeholder="操作符号类型，英文"></el-input>
      </el-form-item>
    <el-form-item label="操作符号类型，英文" prop="type">
      <el-input v-model="dataForm.type" placeholder="操作符号类型，英文"></el-input>
    </el-form-item>
    <el-form-item label="操作符中文名" prop="typeChineseName">
      <el-input v-model="dataForm.typeChineseName" placeholder="操作符中文名"></el-input>
    </el-form-item>
    <el-form-item label="sql对应操作" prop="sqlFlag">
      <el-input v-model="dataForm.sqlFlag" placeholder="sql对应操作"></el-input>
    </el-form-item>
    <el-form-item label="详细描述" prop="typeDes">
      <el-input v-model="dataForm.typeDes" placeholder="详细描述"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="删除标志" prop="deleteFlag">
      <el-input v-model="dataForm.deleteFlag" placeholder="删除标志"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          fieldType: 0,
          type: '',
          typeChineseName: '',
          sqlFlag: '',
          typeDes: '',
          remark: '',
          deleteFlag: ''
        },
        dataRule: {
          type: [
            { required: true, message: '操作符号类型，英文不能为空', trigger: 'blur' }
          ],
          typeChineseName: [
            { required: true, message: '操作符中文名不能为空', trigger: 'blur' }
          ],
          sqlFlag: [
            { required: true, message: 'sql对应操作不能为空', trigger: 'blur' }
          ],
          typeDes: [
            { required: true, message: '详细描述不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          deleteFlag: [
            { required: true, message: '删除标志不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.fieldType = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fieldType) {
            this.$http({
              url: this.$http.adornUrl(`/tag/operationtypedic/info/${this.dataForm.fieldType}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.operationTypeDic.type
                this.dataForm.typeChineseName = data.operationTypeDic.typeChineseName
                this.dataForm.sqlFlag = data.operationTypeDic.sqlFlag
                this.dataForm.typeDes = data.operationTypeDic.typeDes
                this.dataForm.remark = data.operationTypeDic.remark
                this.dataForm.deleteFlag = data.operationTypeDic.deleteFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tag/operationtypedic/${!this.dataForm.fieldType ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fieldType': this.dataForm.fieldType || undefined,
                'type': this.dataForm.type,
                'typeChineseName': this.dataForm.typeChineseName,
                'sqlFlag': this.dataForm.sqlFlag,
                'typeDes': this.dataForm.typeDes,
                'remark': this.dataForm.remark,
                'deleteFlag': this.dataForm.deleteFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
